package com.ruoyi.web.controller.societies;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.PostDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Post;
import com.ruoyi.system.domain.Comment;
import com.ruoyi.system.service.IPostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 帖子管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-25
 */
@RestController
@RequestMapping("/system/societies/post")
public class PostController extends BaseController
{
    @Autowired
    private IPostService postService;

    /**
     * 查询帖子列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(Post post)
    {
        List<Post> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 导出帖子列表
     */
    @Log(title = "帖子管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Post post)
    {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        util.exportExcel(response, list, "帖子数据");
    }

    /**
     * 获取帖子详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(postService.selectPostById(id));
    }

    /**
     * 新增帖子
     */
    @Log(title = "帖子管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostDTO post)
    {
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改帖子
     */
    @Log(title = "帖子管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostDTO postDTO)
    {
        Post post = BeanUtil.toBean(postDTO, Post.class);
        post.setIsTop(postDTO.getIsTop() ? 1 : 0);
        // 设置作者ID为当前用户
        post.setAuthorId(SecurityUtils.getUserId());
        post.setCreatedAt(new Date());
        post.setUpdatedAt(new Date());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除帖子
     */
    @Log(title = "帖子管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(postService.deletePostByIds(ids));
    }

    /**
     * 获取用户可发帖的社团列表
     */
    @GetMapping("/userClubs")
    public AjaxResult getUserClubs()
    {
        return success(postService.getUserClubs());
    }

    /**
     * 查询评论列表
     */
    @GetMapping("/comments")
    public TableDataInfo listComments(Comment comment)
    {
        List<Comment> list = postService.selectCommentList(comment);
        return getDataTable(list);
    }

    /**
     * 发表评论
     */
    @PostMapping("/comment")
    public AjaxResult addComment(@RequestBody Comment comment)
    {
        return toAjax(postService.insertComment(comment));
    }

    /**
     * 点赞评论
     */
    @PostMapping("/likeComment/{commentId}")
    public AjaxResult likeComment(@PathVariable("commentId") Long commentId)
    {
        return toAjax(postService.likeComment(commentId));
    }

    /**
     * 点赞帖子
     */
    @PostMapping("/likePost/{postId}")
    public AjaxResult likePost(@PathVariable("postId") Long postId)
    {
        return toAjax(postService.likePost(postId));
    }

    /**
     * 增加浏览量
     */
    @PostMapping("/view/{postId}")
    public AjaxResult increaseViewCount(@PathVariable("postId") Long postId)
    {
        return toAjax(postService.increaseViewCount(postId));
    }
} 